package com.ruoyi.common.utils;


import com.ruoyi.common.core.domain.hospital.MrInspection;
import com.ruoyi.common.core.domain.hospital.MrInspections;
import com.ruoyi.common.core.domain.hospital.MrInspectionsData;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析mrInspectionsData.xml 工具类
 *  检查报告数据集
 */
public class MrInspectionUtils {
    public static List<MrInspection> getMrInspection(String file) throws JAXBException {
        List<MrInspection> mrInspectionList = new ArrayList<>();
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(MrInspectionsData.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            StringReader reader = new StringReader(file);
            MrInspectionsData mrInspectionsData = (MrInspectionsData) jaxbUnmarshaller.unmarshal(reader);
            mrInspectionList = mrInspectionsData.getMrInspections().getMrInspectionList();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return mrInspectionList;
    }

    public static String mrInspectionToXML(List<MrInspection> person) throws JAXBException, TransformerException {

        MrInspectionsData mrInspectionsData = new MrInspectionsData();
        MrInspections mrInspections = new MrInspections();
        mrInspections.setMrInspectionList(person);
        mrInspectionsData.setMrInspections(mrInspections);

        JAXBContext jaxbContext = JAXBContext.newInstance(MrInspectionsData.class);

        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // 输出格式化
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        StringWriter sw = new StringWriter();
        jaxbMarshaller.marshal(mrInspectionsData, sw);

        // 获取生成的XML字符串（包含XML声明头）
        String xmlWithDeclaration = sw.toString();

        // 使用Transformer来移除XML声明头
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        // 由于StringSource不是标准API的一部分，我们使用StreamSource并跳过XML声明
        // 我们通过Transformer直接输出到另一个StringWriter来避免XML声明
        Source inputSource = new javax.xml.transform.stream.StreamSource(new java.io.StringReader(xmlWithDeclaration));
        StringWriter outputStringWriter = new StringWriter();
        StreamResult outputStreamResult = new StreamResult(outputStringWriter);

        // 执行转换
        transformer.transform(inputSource, outputStreamResult);

        // 获取最终的XML字符串（不带XML声明头）
        String xmlWithoutDeclaration = outputStringWriter.toString();

        return xmlWithoutDeclaration;
    }

//    @XmlRootElement(name = "mrInspectionsData")
//    @XmlAccessorType(XmlAccessType.FIELD)
//    static class MrInspections {
//        @XmlElement(name = "mrInInspections")
//        private MrInspections.MrInspectionsWrapper mrInspectionsWrapper;
//
//        static class MrInspectionsWrapper {
//            @XmlElement(name = "MrInInspection")
//            private List<MrInspection> mrInspection;
//
//            public List<MrInspection> getMrInspection() {
//                return mrInspection;
//            }
//
//            public void setMrInspection(List<MrInspection> mrInspection) {
//                this.mrInspection = mrInspection;
//            }
//        }
//
//        public MrInspectionsWrapper getMrInspectionsWrapper() {
//            return mrInspectionsWrapper;
//        }
//
//        public void setMrInspectionsWrapper(MrInspectionsWrapper mrInspectionsWrapper) {
//            this.mrInspectionsWrapper = mrInspectionsWrapper;
//        }
//    }
}
